/**
 * Created by li on 2018-02-26.
 */
/**
 * 判断请求服务是否成功
 **/
var isSuccess = function (data){
    if(data == null){
        return false;
    }
    if(data.code == 100 && data.msg == '成功'){
        return true;
    }
    return false;
}

/**
 * 初始化批量删除方法
 **/
var initBatchDelete = function (grid,url){
    if(grid && grid != null){
        var rows = grid.datagrid("getChecked");
        if(rows == null || rows.length == 0){
            j_tip("请先至少勾选一行数据!","error");
            return;
        }
        var keyIds = "";
        var self = grid[0];
        var list = [];
        for(var i=0;i<rows.length;i++){
            var keyId = self.getFieldValue(rows[i],"o.id");
            list.push({id:keyId,name:self.getFieldValue(rows[i],"o.outerIp")});
            keyIds += (keyIds==""?"":",")+keyId;
        }
        $.messager.confirm('系统提示', '您确认删除选中的'+rows.length+'条数据?', function(r){
            if (r){
                self.focus();
                showLoading();
                fy_api.ajax({
                    type:"POST",
                    url: url + "?_t="+new Date().getTime(),
                    data: {"keyIds":keyIds},
                    success: function(data){
                        hideLoading();
                        debugger;
                        if(isSuccess(data)){
                            $.messager.alert('系统提示',"删除成功,共"+rows.length+"行",'success',function(){
                                grid.datagrid("reload");
                            });
                        }else if(data.content!=null && data.content.length>0){
                            var html = "";
                            for(var i=0;i<data.content.length;i++){
                                var id = data.content[i][0];
                                for(var j=0;j<list.length;j++){
                                    if(list[j].id==id){
                                        html += "<br/>\""+list[j].name+"\":"+data.content[i][1];
                                        break;
                                    }
                                }
                            }
                            if(data.content.length == rows.length){
                                $.messager.alert('系统提示',"删除失败,共"+rows.length+"行:"+html,'hide',function(){
                                    self.focus();
                                });
                            }else{
                                $.messager.alert('系统提示',"部分删除成功,成功"+(rows.length-data.content.length)+"行,失败"+data.content.length+"行:"+html,'hide',function(){
                                    grid.datagrid("reload");
                                });
                            }
                        }
                    }
                });
            }else{
                self.focus();
            }
        });
    }
}

/**
 * 初始化批量删除方法
 * keyField  主键字段名
 * tipField  提示字段名
 **/
var initBatchDelete_2 = function (grid,url,keyField,tipField){
    if(grid && grid != null){
        var rows = grid.datagrid("getChecked");
        if(rows == null || rows.length == 0){
            j_tip("请先至少勾选一行数据!","error");
            return;
        }
        var keyIds = "";
        var self = grid[0];
        var list = [];
        for(var i=0;i<rows.length;i++){
            var keyId = self.getFieldValue(rows[i],"o."+keyField);
            list.push({id:keyId,name:self.getFieldValue(rows[i],"o."+tipField)});
            keyIds += (keyIds==""?"":",")+keyId;
        }
        $.messager.confirm('系统提示', '您确认删除选中的'+rows.length+'条数据?', function(r){
            if (r){
                self.focus();
                showLoading();
                fy_api.ajax({
                    type:"POST",
                    url: url + "?_t="+new Date().getTime(),
                    data: {"keyIds":keyIds},
                    success: function(data){
                        hideLoading();
                        debugger;
                        if(isSuccess(data)){
                            $.messager.alert('系统提示',"删除成功,共"+rows.length+"行",'success',function(){
                                grid.datagrid("reload");
                            });
                        }else if(data.content!=null && data.content.length>0){
                            var html = "";
                            for(var i=0;i<data.content.length;i++){
                                var id = data.content[i][0];
                                for(var j=0;j<list.length;j++){
                                    if(list[j].id==id){
                                        html += "<br/>\""+list[j].name+"\":"+data.content[i][1];
                                        break;
                                    }
                                }
                            }
                            if(data.content.length == rows.length){
                                $.messager.alert('系统提示',"删除失败,共"+rows.length+"行:"+html,'hide',function(){
                                    self.focus();
                                });
                            }else{
                                $.messager.alert('系统提示',"部分删除成功,成功"+(rows.length-data.content.length)+"行,失败"+data.content.length+"行:"+html,'hide',function(){
                                    grid.datagrid("reload");
                                });
                            }
                        }
                    }
                });
            }else{
                self.focus();
            }
        });
    }
}

/**
 * 初始化批量操作方法
 **/
var initBatchOperation = function (grid,url,operationType){
    if(grid && grid != null){
        if(undefined == operationType){
            operationType = "删除";
        }
        var rows = grid.datagrid("getChecked");
        if(rows == null || rows.length == 0){
            j_tip("请先至少勾选一行数据!","error");
            return;
        }
        var keyIds = "";
        var self = grid[0];
        var list = [];
        for(var i=0;i<rows.length;i++){
            var keyId = self.getFieldValue(rows[i],"o.id");
            list.push({id:keyId,name:self.getFieldValue(rows[i],"o.outerIp")});
            keyIds += (keyIds==""?"":",")+keyId;
        }
        $.messager.confirm('系统提示', '您确认' + operationType + '选中的'+rows.length+'条数据?', function(r){
            if (r){
                self.focus();
                showLoading();
                fy_api.ajax({
                    type:"POST",
                    url: url + "?_t="+new Date().getTime(),
                    data: {"keyIds":keyIds},
                    success: function(data){
                        hideLoading();
                        debugger;
                        if(isSuccess(data)){
                            $.messager.alert('系统提示',operationType + "成功,共"+rows.length+"行",'success',function(){
                                grid.datagrid("reload");
                            });
                        }else if(data.content!=null && data.content.length>0){
                            var html = "";
                            for(var i=0;i<data.content.length;i++){
                                var id = data.content[i][0];
                                for(var j=0;j<list.length;j++){
                                    if(list[j].id==id){
                                        html += "<br/>\""+list[j].name+"\":"+data.content[i][1];
                                        break;
                                    }
                                }
                            }
                            if(data.content.length == rows.length){
                                $.messager.alert('系统提示',operationType + "失败,共"+rows.length+"行:"+html,'hide',function(){
                                    self.focus();
                                });
                            }else{
                                $.messager.alert('系统提示',"部分" + operationType + "成功,成功"+(rows.length-data.content.length)+"行,失败"+data.content.length+"行:"+html,'hide',function(){
                                    grid.datagrid("reload");
                                });
                            }
                        }
                    }
                });
            }else{
                self.focus();
            }
        });
    }
}